package com.atguigu.classroom.vod.utils;

import com.qiniu.common.QiniuException;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.util.Auth;

import java.util.List;
import java.util.stream.Collectors;

public class QiniuUtils {
    public static void deleteFileList(List<String> urls){
        List<String> fileKeys=urls.stream().map(url->{
            String[] keys = url.split(ConstantPropertiesUtil.URL);
            return keys[keys.length-1];
        }).collect(Collectors.toList());
        Auth auth=Auth.create(ConstantPropertiesUtil.ACCESSKEY,ConstantPropertiesUtil.SECRETKEY);
        Configuration cfg=new Configuration(Region.region1());
        BucketManager bucketManager=new BucketManager(auth,cfg);
        BucketManager.BatchOperations batchOperations=new BucketManager.BatchOperations();
        String[] files=new String[fileKeys.size()];
        batchOperations.addDeleteOp(ConstantPropertiesUtil.BUCKET,fileKeys.toArray(files));
        try {
            bucketManager.batch(batchOperations);
        } catch (QiniuException e) {
            e.printStackTrace();
        }
    }
}
